const { addCustomers,getCustomers,getCustomersBySalesmansId,deleteCustomers,sureAlterCustomers,getUsers } = require('../dao/customersDao')

//新增用户
module.exports.addCustomers = async customerData =>{
    const data = await addCustomers(customerData);
   
    if (data._id) {
        // console.log(data);
        return {
            message: '添加成功',
            status: 1
        }
    } else {
        return {
            message: '添加失败',
            status: 0
        }
    }
}

//获取所有用户
module.exports.getCustomers = async allData => {
    const data = await getCustomers(allData);
    if (data.data.length > 0) {
        return {
            message: '获取成功',
            status: 1,
            data
        }
    } else {
        return {
            message: '数据库没有数据，页面渲染失败',
            status: 0,
            data
        }
    }
}

//通过销售员Id获取用户
module.exports.getCustomersBySalesmansId = async idData => {
    const data = await getCustomersBySalesmansId(idData);
    if(data.length>0){
        return {
            message: '获取成功',
            status: 1,
            data
        }
    }
    return {
        message: '数据库没有数据，页面渲染失败',
        status: 0,
        data
    }
}
//删除用户
module.exports.deleteCustomers = async idData => {
    const data = await deleteCustomers(idData);
    if (data.nModified > 0) {
        return {
            message: '删除成功',
            status: 1
        }
    } else {
        return {
            message: '删除失败',
            status: 0
        }
    }
}

//通过id获取用户数据
// module.exports.alterCustomers = async idData => {
//     return await alterCustomers(idData);

// }

//修改用户数据
module.exports.sureAlterCustomers = async alterData => {
    const data = await sureAlterCustomers(alterData);
    if (data.nModified > 0) {
        return {
            message: '修改成功',
            status: 1
        }
    } else {
        return {
            message: '修改失败',
            status: 0
        }
    }

}

//通过用户手机号获取用户
const jwt = require('jsonwebtoken')
module.exports.getUsers = async usersData => {
    const data = await getUsers(usersData);
    if(data.length>0){
        const {phone,password} = usersData
        const token = jwt.sign(
            {phone},
            'admins',   //密钥
            {expiresIn: 60*10} //token有效期
        )
        return {
            data,token
        }
    }
}